package cn.pengpeng;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.mapreduce.*;
import org.apache.hadoop.util.ReflectionUtils;
import org.apache.log4j.Logger;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class MySQLMapper extends Mapper<LongWritable, MySQLMapperInputValue, MySQLMapperOutputKey, MySQLMapperOutputValue> {

    private Calendar calendar = Calendar.getInstance();
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    private MySQLMapperOutputKey outputKey;
    private MySQLMapperOutputValue outputValue;

    @Override
    protected void map(LongWritable key, MySQLMapperInputValue value, Context context)
            throws IOException, InterruptedException {

        outputKey = new MySQLMapperOutputKey();
        outputKey.setUrl(value.getUrl());
        calendar.setTimeInMillis(value.getTime());
        outputKey.setDate(sdf.format(calendar.getTime()));

        outputValue = new MySQLMapperOutputValue();
        outputValue.setUid(value.getUid());
        outputValue.setSid(value.getSid());

        System.out.println(key);

        context.write(outputKey, outputValue);
    }
}